
/**
  *Copyright(C),2020 - 2021, Company Tech. Co., Ltd.
  *FileName:   serial_trans.c
  *Date:       2025-02-15 11:11:34
  *Author:     maguoqiang
  *Version:    1.0
  *Path:       D:\project_document\serial_transmission\project\applications
  *Description:实现串口透传功能,串口1用于接受上位机的数据,串口3用于把数据传出
*/

/* include ------------------------------------------------------------------*/
#include "serial_trans.h"
/* private define -----------------------------------------------------------*/



/* private variables --------------------------------------------------------*/



/* public function prototype ------------------------------------------------*/

/**
  * @brief  串口初始化
  * @param  None
  * @retval None
  * @note   需要保证串口时钟及引脚配置完毕
*/
void serialInit(rt_device_t uart_device)
{
    rt_err_t result = 0;
	struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    result = rt_device_control(uart_device,RT_DEVICE_CTRL_CONFIG,&config);
    RT_ASSERT(result == RT_EOK);
    result = rt_device_open(uart_device,RT_DEVICE_FLAG_RX_NON_BLOCKING|RT_DEVICE_FLAG_TX_BLOCKING);
    RT_ASSERT(result == RT_EOK);



}

/**
  * @brief  串口发送数据
  * @param  dev     执行操作的串口设备
  * @param  buffer  存放要发送的数据的缓冲区
  * @param  size    写入数据的大小
  * @retval 已发送的数据量，单位为字节
  * @note   None
*/
rt_uint32_t serialSend(rt_device_t dev, const void* buffer, rt_size_t size)
{
    rt_uint32_t send_data_count = 0;
    send_data_count = rt_device_write(dev,0,buffer,size);
    return send_data_count;
}


/**
  * @brief  串口接收数据
  * @param  dev     执行操作的串口设备
  * @param  buffer  存放要发送的数据的缓冲区
  * @param  size    写入数据的大小
  * @retval 接收到的数据量，单位为字节
  * @note   None
*/
rt_uint32_t serialReceive(rt_device_t dev, void* buffer, rt_size_t size)
{
    rt_uint32_t recv_data_count = 0;
    recv_data_count = rt_device_read(dev,0,buffer,size);
    return recv_data_count;
}

/// @brief 设置串口接收的回调函数
/// @param dev 串口设备
/// @param rx_ind 回调函数
void setUartRXHandler(rt_device_t dev,rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
{
	rt_err_t result = 0;
    result = rt_device_set_rx_indicate(dev,rx_ind);
    RT_ASSERT(result == RT_EOK);
}
/* private function prototype -----------------------------------------------*/

